package de.tabuma.sso.usergroup;

import de.tabuma.sso.*;
import de.tabuma.sso.sso.*;
import de.tabuma.sso.group.*;
import de.tabuma.sso.user.*;
import de.tabuma.shopping.logik.*;
import java.util.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.2.1 $
 * @since $Date: 2010/01/08 10:03:24 $
 */
public class UserGroupWebservice {
    public static int create(UserGroup userGroup, String ssoId) throws Exception {
        int groupId = UserGroupWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.USERGROUP_CREATE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }
        
        UserGroupWebserviceLib.userHasPrivilegForUserGroup(userGroup, ssoId);
        return UserGroupFacade.create(userGroup, user.getName());
    }

    public static void update(UserGroup userGroup, String ssoId) throws Exception {
        int groupId = UserGroupWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.USERGROUP_UPDATE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        UserGroupWebserviceLib.userHasPrivilegForUserGroup(userGroup, ssoId);
        UserGroup oldUserGroup = UserGroupFacade.get(userGroup.getId().intValue());
        UserGroupWebserviceLib.userHasPrivilegForUserGroup(oldUserGroup, ssoId);
        UserGroupFacade.update(userGroup, user.getName());
    }

    public static UserGroup get(int id, String ssoId) throws Exception {
        int groupId = UserGroupWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.USERGROUP_GET, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        UserGroup userGroup = UserGroupFacade.get(id);
        UserGroupWebserviceLib.userHasPrivilegForUserGroup(userGroup, ssoId);
        return userGroup;
    }

    public static void remove(int id, String ssoId) throws Exception {
        int groupId = UserGroupWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.USERGROUP_REMOVE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        UserGroup userGroup = UserGroupFacade.get(id);
        UserGroupWebserviceLib.userHasPrivilegForUserGroup(userGroup, ssoId);
        UserGroupFacade.remove(id, ssoId);
    }

    public static List<UserGroup> getList(int groupId1, String ssoId) throws Exception {
        int groupId = UserGroupWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.USERGROUP_GETLIST, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        Group group = GroupFacade.get(groupId1);
        GroupWebserviceLib.userHasPrivilegForGroup(group, ssoId);
        return UserGroupFacade.getList(groupId1);
    }
}